package vip.youngboy.graph;

public class EqUnion {

    public static void main(String[] args) {
        String[] test=new String[]{"a==b","b==c","a==c"};
        boolean b = equationsPossible(test);
        System.out.println(b);
    }

    public static boolean equationsPossible(String[] equations) {
        //26个字母
        UF uf = new UF(26);
        //把==都联通起来
        for(String eq:equations){
            if(eq.charAt(1)=='='){
                int p=eq.charAt(0);
                int q=eq.charAt(3);
                //-a 从 0 开始
                uf.union(p-'a',q-'a');
            }
        }

        //处理!=
        for(String eq:equations){
            if(eq.charAt(1)=='!'){
                int p=eq.charAt(0);
                int q=eq.charAt(3);
                //-a 从 0 开始
                if(uf.connected(p-'a',q-'a')){
                    return false;
                }
            }
        }

        return true;
    }

}


